﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Manager.DataAccess;
using Manager.FormAdd_Edit;
using Manager.App_Code;
using System.Linq;
using Manager.Common;
using System.Data.Linq;
using Manager.FormMain;

namespace Manager
{
    public partial class Room : UserControl
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        //dang ky event for chuyen phong
        public delegate void DataChangedEventHandler(object sender, DataChangedEventArgs e);
        public DataChangedEventHandler DataChanged;
        public string Name
        {
            get { return butRoom.Text; }
            set { butRoom.Text = value; }
        }        

        public Room()
        {
            InitializeComponent();
            Name = "";
        }

        public Room(string name, string _MaPhong)
        {
            InitializeComponent();
            Name = name;
            MaPhong = _MaPhong;
        }

        public void Room_Load(object sender, EventArgs e)
        {
            try
            {                
                db.Refresh(RefreshMode.OverwriteCurrentValues, db.Phongs);
                var dr = db.Phongs.Single(i => i.MaPhong == MaPhong);
                var loaiPhong = db.LoaiPhongs.Single(i => i.MaLoaiPhong == dr.LoaiPhong);
                var tinhTrang = CommonFunction.ParseInt(dr.TinhTrang);
                SetStatusRoom(dr, tinhTrang, loaiPhong);                
            }
            catch (Exception ex)
            {
                Log.WriteLog("Room:Room_Load " + ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!");
            }
        }

        public void butRoom_DoubleClick()
        {
            frm_RoomInfo roomInfo = new frm_RoomInfo(MaPhong);
            roomInfo.Show();
        }
     
        public string MaPhong { get; set; }
        public int MaTang { get; set; }
        public string DienThoai { get; set; }
        public string TenPhong { get { return Name; } set { Name = value; } }
        public string LoaiPhong { get; set; }
        public string TinhTrang { get; set; }
        public string GhiChu { get; set; }
        public int ViTri { get; set; }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataRoom dr = new DataRoom();
            dr.MaPhong = MaPhong;
            (Parent as IRemovable).RemoveRoom(dr);
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }      

        private void butRoom_Click(object sender, EventArgs e)
        {
            try
            {
                var dr = db.Phongs.Single(i => i.MaPhong == MaPhong);
                if (dr.TinhTrang == ((int)CommonEnum.TinhTrangPhong.CoKhach_3).ToString() || dr.TinhTrang == ((int)CommonEnum.TinhTrangPhong.ToiHan_4).ToString())
                {
                    var phieudk = db.PhieuDangKyPhongs.SingleOrDefault(i => i.ChiTietPhieuDangKy.MaPhong == MaPhong && i.TinhTrang.Value == false);
                    (ParentForm as IDatatranfer).TranferData(dr, phieudk);
                }
                else
                {
                    (ParentForm as IDatatranfer).TranferData(dr, null);
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("butRoom_Click: " + ex.Message + "\n" + ex.StackTrace);
            }
        }

        private void SanSangToolStripMenuItem_Click(object sender, EventArgs e)
        {

            var phong = db.Phongs.Single(i => i.MaPhong == MaPhong);            
            phong.TinhTrang = ((int)CommonEnum.TinhTrangPhong.SanSang_1).ToString();
            db.SubmitChanges();            
            Room_Load(sender, e);
        }

        private void SuaChuaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var phong = db.Phongs.Single(i => i.MaPhong == MaPhong);
            phong.TinhTrang = ((int)CommonEnum.TinhTrangPhong.SuaChua_6).ToString();
            db.SubmitChanges();
            Room_Load(sender, e);
        }

        private void ChuaDonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var phong = db.Phongs.Single(i => i.MaPhong == MaPhong);
            phong.TinhTrang = ((int)CommonEnum.TinhTrangPhong.ChuaDon_2).ToString();
            db.SubmitChanges();
            Room_Load(sender, e);
        }        

        private void butRoom_MouseHover(object sender, EventArgs e)
        {
            //butRoom.BackColor = Color.Transparent;
        }

        private void DatPhongToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_RegistryRoom regis = new frm_RegistryRoom(null);
            regis.ShowDialog();
        }

        private void checkOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_CheckOutMain frmcheckoutmain = new frm_CheckOutMain(MaPhong);
            frmcheckoutmain.ShowDialog();
        }

        private void HoaDonDichVuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_HoaDonDichVu frmhoadon = new frm_HoaDonDichVu(MaPhong);
            frmhoadon.ShowDialog();
        }

        private void DangKyKhachLeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_DKKhachLe frmdkyKL = new frm_DKKhachLe(MaPhong);
            frmdkyKL.FormClosed += new FormClosedEventHandler(frmdkyKL_FormClosed);
            frmdkyKL.ShowDialog();
        }

        void frmdkyKL_FormClosed(object sender, FormClosedEventArgs e)
        {
            var frm = sender as frm_DKKhachLe;
            if (frm != null)
            {
                if (DataChanged != null)
                    DataChanged(this, new DataChangedEventArgs() { Message = "Change data." });
            }
        }

        private void DangKyKhachDoanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_DKKhachDoan khachdoan = new frm_DKKhachDoan();
            khachdoan.ShowDialog();
        }

        private void DangKyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ct_ctPhieuDatPhong = db.ChiTiet_ChiTietPhieuDatPhongs.SingleOrDefault(t => t.MaPhong == MaPhong);
            if (ct_ctPhieuDatPhong != null)
            {
                var nhomKhach = ct_ctPhieuDatPhong.ChiTietPhieuDatPhong.PhieuDatPhong.NhomKhachDatPhong;
                if (nhomKhach == null)
                {
                    frm_DKKhachLe frmdkyKL = new frm_DKKhachLe(ct_ctPhieuDatPhong.ChiTietPhieuDatPhong.PhieuDatPhong, MaPhong);
                    frmdkyKL.ShowDialog();
                }
                else
                {
                    frm_DKKhachDoan frm_DKKD = new frm_DKKhachDoan(nhomKhach);
                    frm_DKKD.ShowDialog();
                }
                if (DataChanged != null)
                    DataChanged(this, new DataChangedEventArgs() { Message = "Change data." });
            }
            else
            {
                frm_DKKhachLe frmdkyKL = new frm_DKKhachLe(MaPhong);
                frmdkyKL.ShowDialog();
            }

            (ParentForm as IDatatranfer).Frm_RoomDiagram_Load(null, null);
        }        

        private void InPhieuDangKyToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void HieuChinhDangKyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var phieuDangKy = db.PhieuDangKyPhongs.SingleOrDefault(i => i.ChiTietPhieuDangKy.MaPhong == MaPhong && i.TinhTrang == false);
            frm_DKKhachLe dangky = new frm_DKKhachLe(phieuDangKy);
            //dangky.FormClosed += new FormClosedEventHandler(dangky_FormClosed);
            dangky.ShowDialog();
            (ParentForm as IDatatranfer).Frm_RoomDiagram_Load(null, null);
        }

        void dangky_FormClosed(object sender, FormClosedEventArgs e)
        {
            (ParentForm as IDatatranfer).Frm_RoomDiagram_Load(null, null);
        }

        private void SetStatusRoom(Phong dr, int tinhTrang, LoaiPhong loaiPhong)
        {
            try
            {
                #region San sang
                if (tinhTrang == (int)CommonEnum.TinhTrangPhong.SanSang_1)
                {
                    butRoom.BackColor = Color.White;
                    butRoom.ForeColor = Color.Blue;

                    butRoom.ContextMenuStrip = contextMenuStrip_SanSang;

                    chưaDọnToolStripMenuItem.Checked = false;
                    SanSangToolStripMenuItem.Checked = true;
                    sửaChữaToolStripMenuItem.Checked = false;

                    SanSangtoolMenuItem.Checked = true;
                    ChuaDontoolMenuItem.Checked = false;
                    SuaChuatoolMenuItem.Checked = false;
                }
                #endregion
                #region chua don
                else if (tinhTrang == (int)CommonEnum.TinhTrangPhong.ChuaDon_2)
                {
                    butRoom.BackColor = Color.Yellow;
                    butRoom.ForeColor = Color.Black;

                    butRoom.ContextMenuStrip = contextMenuStrip_Other;

                    chưaDọnToolStripMenuItem.Checked = true;
                    SanSangToolStripMenuItem.Checked = false;
                    sửaChữaToolStripMenuItem.Checked = false;

                    SanSangtoolMenuItem.Checked = false;
                    ChuaDontoolMenuItem.Checked = true;
                    SuaChuatoolMenuItem.Checked = false;
                }
                #endregion
                #region co khach
                else if (tinhTrang == (int)CommonEnum.TinhTrangPhong.CoKhach_3)
                {
                    butRoom.BackColor = Color.Blue;
                    butRoom.ForeColor = Color.White;

                    butRoom.ContextMenuStrip = contextMenuStrip_CoKhach;

                    var phieudk = db.PhieuDangKyPhongs.SingleOrDefault(i => i.ChiTietPhieuDangKy.MaPhong == dr.MaPhong && i.TinhTrang.Value == false);
                    if (phieudk.NgayDi.Value <= DateTime.Now)
                    {
                        dr.TinhTrang = ((int)CommonEnum.TinhTrangPhong.ToiHan_4).ToString();
                        db.SubmitChanges();
                        db.Refresh(RefreshMode.OverwriteCurrentValues, db.Phongs);
                        butRoom.BackColor = Color.Red;
                        butRoom.ForeColor = Color.White;

                        butRoom.ContextMenuStrip = contextMenuStrip_CoKhach;
                    }
                }
                #endregion
                #region cho khach
                else if (tinhTrang == (int)CommonEnum.TinhTrangPhong.ChoKhach_5)
                {
                    butRoom.BackColor = Color.Lime;
                    butRoom.ForeColor = Color.Blue;

                    butRoom.ContextMenuStrip = contextMenuStrip_ChoKhach;
                }
                #endregion
                #region toi han
                else if (tinhTrang == (int)CommonEnum.TinhTrangPhong.ToiHan_4)
                {
                    butRoom.BackColor = Color.Red;
                    butRoom.ForeColor = Color.White;
                    var phieudk = db.PhieuDangKyPhongs.SingleOrDefault(i => i.ChiTietPhieuDangKy.MaPhong == dr.MaPhong && i.TinhTrang.Value == false);
                    if (phieudk.NgayDi.Value >= DateTime.Now)
                    {

                        dr.TinhTrang = ((int)CommonEnum.TinhTrangPhong.CoKhach_3).ToString();
                        db.SubmitChanges();
                        db.Refresh(RefreshMode.OverwriteCurrentValues, db.Phongs);
                        butRoom.BackColor = Color.Blue; ;
                        butRoom.ForeColor = Color.White;

                        butRoom.ContextMenuStrip = contextMenuStrip_CoKhach;

                    }

                    butRoom.ContextMenuStrip = contextMenuStrip_CoKhach;
                }
                #endregion
                #region sua chua
                else if (tinhTrang == (int)CommonEnum.TinhTrangPhong.SuaChua_6)
                {
                    butRoom.BackColor = Color.Violet;
                    butRoom.ForeColor = Color.Blue;

                    butRoom.ContextMenuStrip = contextMenuStrip_Other;

                    chưaDọnToolStripMenuItem.Checked = false;
                    SanSangToolStripMenuItem.Checked = false;
                    sửaChữaToolStripMenuItem.Checked = true;

                    SanSangtoolMenuItem.Checked = false;
                    ChuaDontoolMenuItem.Checked = false;
                    SuaChuatoolMenuItem.Checked = true;


                }
                #endregion
                lblMaPhong.Text = loaiPhong.TenLoaiPhong;
            }
            catch (Exception ex)
            {
                Log.WriteLog("SetStatusRoom (Phòng: " + MaPhong + ") " + ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!");
            }
        }

        private void DangKyKhachDoanToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            frm_DKKhachDoan dangkynhom = new frm_DKKhachDoan();
            dangkynhom.ShowDialog();
            (ParentForm as IDatatranfer).Frm_RoomDiagram_Load(null, null);
        }

        private void HieuChinhPhongToolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void SanSangtoolMenuItem_Click(object sender, EventArgs e)
        {
            var phong = db.Phongs.SingleOrDefault(t => t.MaPhong == MaPhong);
            if (phong != null)
            {                
                phong.TinhTrang = ((int)CommonEnum.TinhTrangPhong.SanSang_1).ToString();
                db.SubmitChanges();
                Room_Load(null, null);
            }
        }

        private void SuaChuatoolMenuItem_Click(object sender, EventArgs e)
        {
            var phong = db.Phongs.SingleOrDefault(t => t.MaPhong == MaPhong);
            if (phong != null)
            {
                phong.TinhTrang = ((int)CommonEnum.TinhTrangPhong.SuaChua_6).ToString();
                db.SubmitChanges();
                Room_Load(null, null);
            }
        }

        private void ChuaDontoolMenuItem_Click(object sender, EventArgs e)
        {
            var phong = db.Phongs.SingleOrDefault(t => t.MaPhong == MaPhong);
            if (phong != null)
            {
                phong.TinhTrang = ((int)CommonEnum.TinhTrangPhong.ChuaDon_2).ToString();
                db.SubmitChanges();
                Room_Load(null, null);
            }
        }

        private void checkoutKhachLeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var phieuDangKy = db.PhieuDangKyPhongs.SingleOrDefault(i => i.ChiTietPhieuDangKy.MaPhong == MaPhong && i.TinhTrang == false);
            frm_CheckOutMain frmcheckoutmain = new frm_CheckOutMain(phieuDangKy);
            frmcheckoutmain.ShowDialog();
            if (DataChanged != null)
                DataChanged(this, new DataChangedEventArgs() { Message = "Change data." });
        }

        private void checkoutTheoĐoànToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var phieuDangKy = db.PhieuDangKyPhongs.SingleOrDefault(i => i.ChiTietPhieuDangKy.MaPhong == MaPhong && i.TinhTrang == false);
            if (phieuDangKy != null)
            {
                if (phieuDangKy.MaNhomKhach == null)
                {
                    frm_CheckOutMain frmcheckoutmain = new frm_CheckOutMain(phieuDangKy);
                    frmcheckoutmain.ShowDialog();
                }
                else
                {
                    var lstPhieuDangKy = db.PhieuDangKyPhongs.Where(t => t.TinhTrang == false && t.MaNhomKhach == phieuDangKy.MaNhomKhach).ToList();
                    frm_CheckOutMain frmcheckoutmain = new frm_CheckOutMain(lstPhieuDangKy);
                    frmcheckoutmain.ShowDialog();
                }
                if (DataChanged != null)
                    DataChanged(this, new DataChangedEventArgs() { Message = "Change data." });
            }
        }

        private void ChoKhachToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var phong = db.Phongs.Single(i => i.MaPhong == MaPhong);
            phong.TinhTrang = ((int)CommonEnum.TinhTrangPhong.ChoKhach_5).ToString();
            db.SubmitChanges();
            Room_Load(sender, e);
        }

        private void ChoKhach_CapNhatTinhTrangPhong_SanSan_Click(object sender, EventArgs e)
        {
            var phong = db.Phongs.Single(i => i.MaPhong == MaPhong);
            if (phong.TinhTrang == ((int)CommonEnum.TinhTrangPhong.ChoKhach_5).ToString())
            {
                var chiTiet_ChiTietPhieuDatPhong = db.ChiTiet_ChiTietPhieuDatPhongs.SingleOrDefault(t => t.MaPhong == MaPhong);
                if (chiTiet_ChiTietPhieuDatPhong != null)
                {
                    chiTiet_ChiTietPhieuDatPhong.MaPhong = "";
                    chiTiet_ChiTietPhieuDatPhong.TrangThai = false;
                }
            }
            phong.TinhTrang = ((int)CommonEnum.TinhTrangPhong.SanSang_1).ToString();
            db.SubmitChanges();
            Room_Load(sender, e);
        }

        private void ChoKhach_CapNhatTinhTrangPhong_SuaChua_Click(object sender, EventArgs e)
        {
            var phong = db.Phongs.Single(i => i.MaPhong == MaPhong);
            if (phong.TinhTrang == ((int)CommonEnum.TinhTrangPhong.ChoKhach_5).ToString())
            {
                var chiTiet_ChiTietPhieuDatPhong = db.ChiTiet_ChiTietPhieuDatPhongs.SingleOrDefault(t => t.MaPhong == MaPhong);
                if (chiTiet_ChiTietPhieuDatPhong != null)
                {
                    chiTiet_ChiTietPhieuDatPhong.MaPhong = "";
                    chiTiet_ChiTietPhieuDatPhong.TrangThai = false;
                }
            }
            phong.TinhTrang = ((int)CommonEnum.TinhTrangPhong.SuaChua_6).ToString();
            db.SubmitChanges();
            Room_Load(sender, e);
        }

        private void ChoKhach_CapNhatTinhTrangPhong_ChuaDon_Click(object sender, EventArgs e)
        {
            var phong = db.Phongs.Single(i => i.MaPhong == MaPhong);
            if (phong.TinhTrang == ((int)CommonEnum.TinhTrangPhong.ChoKhach_5).ToString())
            {
                var chiTiet_ChiTietPhieuDatPhong = db.ChiTiet_ChiTietPhieuDatPhongs.SingleOrDefault(t => t.MaPhong == MaPhong);
                if (chiTiet_ChiTietPhieuDatPhong != null)
                {
                    chiTiet_ChiTietPhieuDatPhong.MaPhong = "";
                    chiTiet_ChiTietPhieuDatPhong.TrangThai = false;
                }
            }
            phong.TinhTrang = ((int)CommonEnum.TinhTrangPhong.ChuaDon_2).ToString();
            db.SubmitChanges();
            Room_Load(sender, e);
        }

        private void SanSang_ThongTinPhong_Click(object sender, EventArgs e)
        {
            butRoom_DoubleClick();
        }

        private void Other_ThongTinPhong_Click(object sender, EventArgs e)
        {
            butRoom_DoubleClick();
        }

        private void CoKhach_ThongTinPhong_Click(object sender, EventArgs e)
        {
            butRoom_DoubleClick();
        }

        private void ChoKhach_ThongTinPhong_Click(object sender, EventArgs e)
        {
            butRoom_DoubleClick();
        }        
    }

    public class DataChangedEventArgs : EventArgs
    {
        public string Message { get; set; }
    }
}
